package cn.gson.springboot.controller;

import cn.gson.springboot.model.pojos.Employee;
import cn.gson.springboot.model.service.EmployeeService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import java.io.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@Controller
public class EmployeeController {
    @Autowired
    EmployeeService employeeService;

    @RequestMapping("emp-all-list")
    @ResponseBody
    public List<Employee> findAllEmp(String search){
        List<Employee> allEmp = employeeService.findAllEmp(search);
        return allEmp;
    }

    // 所有员工集合分页-wm
    @RequestMapping("emp-list")
    @ResponseBody
    public Map<String,Object> findAllEmps(Integer page ,Integer size,String search,String empTel){
        Page<Object> objects = PageHelper.startPage(page, size);
        List<Employee> allEmp = employeeService.findAllEmp(search);
        Map<String,Object> map = new HashMap<>();
        map.put("total",objects.getTotal());
        map.put("empForm",allEmp);
        return map;
    }

    // 所有员工集合分页-lj
    @RequestMapping("emp-list-lj")
    @ResponseBody
    public Map<String,Object> findAllEmpsLj(Integer page ,Integer size){
        Page<Object> objects = PageHelper.startPage(page, size);
        List<Employee> allEmp = employeeService.selectAllEmp();
        Map<String,Object> map = new HashMap<>();
        map.put("total",objects.getTotal());
        map.put("rows",allEmp);
        return map;
    }
    //所有boss分页
    @RequestMapping("emp-boss-list")
    @ResponseBody
    public Map<String,Object> selectAllBoss(Integer page ,Integer size){
        Page<Object> objects = PageHelper.startPage(page, size);
        List<Employee> allEmp = employeeService.selectAllBoss();
        Map<String,Object> map = new HashMap<>();
        map.put("total",objects.getTotal());
        map.put("rows",allEmp);
        return map;
    }

    @RequestMapping("save-emp")
    @ResponseBody
    public void saveEmp(@RequestBody Employee employee){
        employeeService.saveEmp(employee);
    }


    //查询登录员工
    @RequestMapping("/emp")
    @ResponseBody
    public Employee findEmp(HttpSession session){
        Employee emp = (Employee) session.getAttribute("emp");
        Employee allEmp = employeeService.findEmp(emp.getEmpId());
        return allEmp;
    }

    //模糊查询员工
    @RequestMapping("/emp-fuzzy-list")
    @ResponseBody
    public List<Employee> findAllEmpByFuzzy(String empName){
        List<Employee> allEmp = employeeService.findAllEmpByFuzzy(empName);
        return allEmp;
    }

    //删除
    @RequestMapping("/delete-emp")
    @ResponseBody
    public void deleteEmp(Integer empId){
        employeeService.deleteEmp(empId);
    }

    @RequestMapping("/dept-emp")
    @ResponseBody
    public Map<String,Object> findDeptEmp(Integer page ,Integer size,Integer deptId){
        Page<Object> objects = PageHelper.startPage(page, size);
        List<Employee> deptEmp = employeeService.findDeptEmp(deptId);
        Map<String,Object> map = new HashMap<>();
        map.put("rows",deptEmp);
        map.put("total",objects.getTotal());
        return map;
    }

    //上传员工头像
    String uri = "E:/upload";
    @PostMapping("/upload")
    @ResponseBody
    public String uploadFile(@RequestParam("file") MultipartFile file, HttpSession session){
        //1、获取文件名，截取扩展名
        String fileName = file.getOriginalFilename();

        String ex = FilenameUtils.getExtension(fileName);
        //2、生成唯一的文件名
        String newName = UUID.randomUUID().toString();
        //3、将文件名与扩展名拼接成新的文件名
        String newPath = newName + "." + ex;
        //拼接完整的文件路径
        File f = new File(uri,newPath);
        if(!f.exists()){
            f.mkdirs();
        }
        try {
            //4、将文件进行转移,将文件从程序内存写入到文件
            file.transferTo(f);
        }catch(Exception e){
            e.printStackTrace();
        }
        //5、将图片路径保存到数据库
        Employee e = (Employee)session.getAttribute("emp");
        employeeService.updateEmpImg("/upload"+newPath,e.getEmpId());
        System.out.println("upload/"+newPath);
        //6、将文件路径立即回显
        return "upload/"+newPath;
    }

    @GetMapping("upload/**")
    public void getImage(HttpServletRequest request,
                         HttpServletResponse response) throws IOException {
        //获得图片的请求路径upload/xxx.jpg
        String servletPath = request.getServletPath();
        //将该路径与dir文件夹路径进行拼接
        String filePath = uri + "/" +servletPath.replaceFirst("/upload/", "");
        //判断路径是否存在
        File f = new File(filePath);
        if(!f.exists()) {
            response.sendError(404);
            return;
        }
        //输入流从文件到内存、输出流内存返回到客户端
        InputStream is = new FileInputStream(f);
        ServletOutputStream sos = response.getOutputStream();
        //复制
        byte[] b = new byte[2048];
        int len = 0;
        while((len = is.read(b)) != -1) {
            sos.write(b,0,len);
        }
        sos.close();
    }

}
